<?php

namespace App\Handler\Admin\Structure\Recruit;

if (!defined('IN_PX'))
    exit;

use App\Handler\Admin\AbstractCommon;
use App\Service\Archives;
use App\Service\Templates;
use App\Service\UPFile;
use App\Tools\Html;
use Phoenix\Support\MsgHelper;
use Exception;

/**
 * 添加
 *
 */
class Add extends AbstractCommon {

    protected function __Inject($db, $cache, $session, UPFile $upFile,
                                Archives $serviceArchives = null, Templates $toolsTemplates = null) {}

    public function processRequest(Array & $context) {
        try {
            $this->db->beginTransaction();
            $this->_pushSetting();


            $_POST['title'] = Html::getTextToHtml($_POST['title']);
            $_POST['is_display'] = isset($_POST['is_display']) ? intval($_POST['is_display']) : 0;
            $_POST['view_count'] = isset($_POST['view_count']) ? intval($_POST['view_count']) : 0;
            $_POST['master_id'] = $this->session->adminUser['id'];
            $_POST['add_date'] = time();
            $_POST['release_date'] = $_POST['add_date'];

            $_identity = $this->db->table('`#@__@recruit`')
                ->row(array(
                    '`title`' => '?',
                    '`synopsis`' => '?',
                    '`cover`' => '?',
                    '`duty`' => '?',
                    '`requirement`' => '?',
                    '`is_display`' => '?',
                    '`view_count`' => '?',
                    '`sort`' => '?',
                    '`seo_title`' => '?',
                    '`seo_keywords`' => '?',
                    '`seo_description`' => '?',
                    '`master_id`' => '?',
                    '`add_date`' => '?',
                    '`release_date`' => '?',
                    '`language`' => '?'
                ))
                ->bind($_POST)
                ->save();

            $this->_createImg('cover');

            $this->db->commit();

            echo(MsgHelper::json($_identity ? 'SUCCESS' : 'DB_ERROR'));
        } catch (Exception $e) {

            $this->db->rollBack();
            echo(MsgHelper::json('DB_ERROR'));
        }
    }

}
